Page({
    data: {
      pop: false,


      bookStationId: '',
      localList: [],
      locationName: '',
      lat: '',
      long: '',

      bookInfoIds: '',
      shipType: '',


      orderId: '',

      cancelInfo: ''


      //地图

      // longitude: 113.324520,
      // latitude: 23.099994,
      // markers: [{
      //   id: 0,
      //   iconPath: "../../images/icon_cur_position.png",
      //   latitude: 23.099994,
      //   longitude: 113.324520,
      //   width: 50,
      //   height: 50
      // }]
    },
    // 放弃本次卖书
  popOpen () {
    this.setData({
      pop:true
    })
  },
  // 狠心取消
  closePop (type) {

    var _this = this
    wx.showLoading({
      title: '订单取消中',
      mask: true
    })
    var params = {
      orderNo: this.data.fromType == 1 ? this.data.orderId: this.data.orderNo
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/donate/cancel/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()
          wx.showToast({
            title: '订单取消成功',
            icon: 'none',
            duration: 2000
          })
          _this.setData({
            pop: false
          })
          setTimeout(function () {
            if (_this.data.fromType==1) {
              wx.navigateBack()
            }else{

              var pages = getCurrentPages(); // 当前页面
              var beforePage = pages[pages.length - 2];

              beforePage.cutOrder(_this.data.orderNo)

              if (type == 1) {
                wx.redirectTo({
                  url: '/package/juanshu/pages/buyBook/buyBook?bookInfoIds=' + JSON.stringify(_this.data.bookInfoIds),
                })
              }else{
                wx.navigateBack()
              }
            }
          },2000)
        }else{

          wx.hideLoading()

          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })

        }
      }
    })
    // this.setData({
    //   pop: false
    // })
  },


  // 取消订单


  // cutOrder () {

  // },


  //获取收书点列表

  getHuishouList(latitude, longitude, city) {
    var _this = this
    var params = {
      pageNum: 1,
      pageSize: 999999,
      city: city,
      latitude: latitude,
      longitude: longitude
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxBookStation/list/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
      "token" : wx.getStorageSync('token'),
        "uid" : wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            localList: res.data.data.resultList,
            bookStationId: res.data.data.resultList[0].id

          })
          var params = {
            bookInfoIds: _this.data.bookInfoIds,
            shipType: _this.data.shipType,
            bookStationId: _this.data.bookStationId,
            openId: wx.getStorageSync('openid')
          }
          if (_this.data.fromType == 1) {
            _this.crateOrder(params)
          }else{
            return
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //打开地图导航
  openMap (e) {
    const latitude = e.currentTarget.dataset.lat

    const longitude = e.currentTarget.dataset.long

    // console.log(latitude)

    // console.log(longitude)

    // return
    const name = e.currentTarget.dataset.name

    const address = e.currentTarget.dataset.address
    var _this = this
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success:function (res) {
        // 30.214600, 120.178520
        wx.openLocation({
          latitude: Number(latitude),//要去的纬度-地址
          longitude: Number(longitude),//要去的经度-地址
          name: name,
          address: address
        })
      }
    })
  },


  // 更换卖书方式
  changeType () {

    if (this.data.fromType == 1) {
      this.closePop(2)
    }else{
      this.closePop(1)
    }
  },


  getScancode () {

    wx.navigateBack()
  },

  //创建订单接口

  crateOrder(params) {
    var _this = this

    wx.showLoading({
      title: '订单创建中',
      mask: true
    })

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/donate/create/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)

        if (res.data.code == 200) {

          wx.hideLoading()
          _this.setData({
            orderId: res.data.data
          })
          wx.showToast({
            title: '订单创建成功,请在卖书列表中查看',
            icon: 'none',
            duration: 2000
          })
          var pages = getCurrentPages(); // 当前页面
          var beforePage = pages[pages.length - 2];
          beforePage.clearList()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  onLoad (options) {

    console.log(options.cancelInfo)
    if (options.fromType == 1) {
      this.setData({
        locationName: options.localname,
        lat: options.lat,
        long: options.long,
        bookInfoIds: options.bookInfoIds,
        shipType: options.shipType,
        fromType: options.fromType,
        cancelInfo: options.cancelInfo
      })
    }

    if (options.fromType == 2) {
      this.setData({
        locationName: wx.getStorageSync('locationName'),
        orderNo: options.orderNo,
        lat: wx.getStorageSync('lat'),
        long: wx.getStorageSync('long'),
        bookInfoIds: JSON.parse(options.bookInfoIds),//有了
        shipType: options.shipType,//有了
        fromType: options.fromType,//有了
        cancelInfo: options.cancelInfo
      })

      console.log(this.data.bookInfoIds)
    }


    // this.setData({
    //   bookInfoIds: options.bookInfoIds,
    //   shipType: options.shipType
    // })

    this.getHuishouList(this.data.lat,this.data.long,this.data.locationName)
  }
})
